package club.ljwang.controller;

import club.ljwang.constant.MessageConstant;
import club.ljwang.entity.Result;
import club.ljwang.pojo.OrderSetting;
import club.ljwang.service.OrderSettingService;
import club.ljwang.utils.POIUtils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wangl
 * @Date: 2020/2/29 20:08
 * @Description:
 */

@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("setnumberbydate")
    public Result setNumberByDate(@RequestBody OrderSetting orderSetting) {


        return orderSettingService.setNumberByDate(orderSetting);

    }

    @RequestMapping("upload")
    public Result upload(MultipartFile excelFile) {

        try {
            List<String[]> dataArray = POIUtils.readExcel(excelFile);
            if (CollectionUtil.isNotEmpty(dataArray)) {
                List<OrderSetting> orderSettings = new ArrayList<>();
                //下面把dataArray数据转为List<OrderSetting>类型的数据传递到Service ，也可以在Service里面做都行
                for (String[] row : dataArray) {
                    if (row.length != 2) {
                        continue;
                    }
                    String dateStr = row[0];
                    String numberStr = row[1];
                    //上面两个值需要校验 ：是不是空字符串 是不是日期 是不是数字 留给你们校验
                    DateTime orderDate = DateUtil.parse(dateStr, "yyyy/MM/dd");
                    Integer number = Integer.valueOf(numberStr);
                    OrderSetting orderSetting = new OrderSetting(orderDate, number);
                    orderSettings.add(orderSetting);
                }
                orderSettingService.addBatch(orderSettings);
            }

            return new Result(true, MessageConstant.UPLOAD_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, "上传文件处理失败!");
        }


    }

    @RequestMapping("/findByDate")
    public Result findByDate(String date) {
        //返回的格式 [{"date":22,"mouth":1,"number":300,"reservations":300,}]
        List<Map> maps = orderSettingService.findByDate(date);
        return new Result(true, "展示页面成功", maps);
    }


}
